import { app, BrowserWindow } from 'electron';
import * as path from 'path';
import { ipcMain } from 'electron';
// import { registerAllIpcHandlers } from '../preload/registerIpc';

function createWindow() {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      preload: path.join(__dirname, '../preload/preload.js'),
      // contextIsolation: true, // 启用上下文隔离 electron 12+ 默认启用
      nodeIntegration: false, // 禁用 Node.js 集成
    },
  });

  console.log('process.env.NODE_ENV', process.env.NODE_ENV);
  console.log('process.env.PORT', process.env.PORT);
  win.loadURL(
    process.env.NODE_ENV === 'development'
      ? `http://localhost:${process.env.PORT || 8003}`
      : `file://${path.join(__dirname, '../renderer/index.html')}`
  );
}

app.whenReady().then(() => {
  createWindow();
  // registerAllIpcHandlers();
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});
